//
//  RegistPwdView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/14.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "RegistPwdView.h"

@interface RegistPwdView()

@property (nonatomic, strong) UITextField *pwd_field;
@property (nonatomic, strong) UITextField *apwd_field;

@end

@implementation RegistPwdView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeRegistPwdView];
    }
    return self;
}

- (void)makeRegistPwdView
{
    UIView *apwdView = [UIView new];
    apwdView.layer.masksToBounds = YES;
    apwdView.layer.borderWidth = 1;
    apwdView.layer.cornerRadius = 10;
    apwdView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:apwdView];
    [apwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 36));
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self);
    }];
    
    _apwd_field = [UITextField new];
    _apwd_field.placeholder = @"再次确认你的密码";
    _apwd_field.secureTextEntry = YES;
    _apwd_field.keyboardType = UIKeyboardTypeASCIICapable;
    _apwd_field.font = [UIFont systemFontOfSize:17.0f];
    [apwdView addSubview:_apwd_field];
    [_apwd_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    UIView *pwdView = [UIView new];
    pwdView.layer.masksToBounds = YES;
    pwdView.layer.borderWidth = 1;
    pwdView.layer.cornerRadius = 10;
    pwdView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:pwdView];
    [pwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 36));
        make.bottom.mas_equalTo(apwdView.mas_top).mas_offset(-20);
        make.centerX.mas_equalTo(self);
    }];
    
    _pwd_field = [UITextField new];
    _pwd_field.placeholder = @"请设置你的密码";
    _pwd_field.secureTextEntry = YES;
    _pwd_field.keyboardType = UIKeyboardTypeASCIICapable;
    _pwd_field.font = [UIFont systemFontOfSize:17.0f];
    [pwdView addSubview:_pwd_field];
    [_pwd_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    UILabel *step_title_label = [UILabel new];
    step_title_label.text = @"步骤: 3  /  3";
    step_title_label.textColor = [UIColor grayColor];
    step_title_label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:step_title_label];
    [step_title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(pwdView.mas_top).mas_offset(-20);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    

    UIButton *trueButton = [UIButton new];
    [trueButton setTitle:@"下一步" forState:UIControlStateNormal];
    [trueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    trueButton.titleLabel.font = [UIFont systemFontOfSize:24.0f];
    [trueButton addTarget:self action:@selector(clickTrueButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:trueButton];
    [trueButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(apwdView.mas_bottom).mas_offset(20);
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
}

- (void)clickTrueButton:(UIButton *)button
{
    if ([NSString isEmptyString:_pwd_field.text] == YES || [NSString isEmptyString:_apwd_field.text] == YES) {
        if ([self.delegate respondsToSelector:@selector(clickTrueWithPwd:)]) {
            [self.delegate clickTrueWithPwd:@0];
        }
    }else if (![_pwd_field.text isEqualToString:_apwd_field.text]){
        if ([self.delegate respondsToSelector:@selector(clickTrueWithPwd:)]) {
            [self.delegate clickTrueWithPwd:@1];
        }
    }else {
        if ([self.delegate respondsToSelector:@selector(clickTrueWithPwd:)]) {
            [self.delegate clickTrueWithPwd:_pwd_field.text];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_apwd_field resignFirstResponder];
    [_pwd_field resignFirstResponder];
}

@end
